'''
Description: 
Autor: didiplus
Date: 2023-01-13 15:01:39
LastEditors: lin
LastEditTime: 2023-01-13 15:35:15
'''


from fastapi import FastAPI,Query
import math
app = FastAPI(debug=True)
from concurrent.futures import ProcessPoolExecutor

def is_prime(num):

    """
    判断是不是素数
    """
    if num < 2:
        return False
    
    if num == 2:
        return True
    
    if num % 2 == 0:
        return False
    sqrt_n  = int(math.floor(math.sqrt(num)))

    for i in range(3,sqrt_n+1,2):
        if num % i == 0:
            return False
    return True

@app.get("/is_prime/{num_list}")
def api_is_prime(num_list:str=Query(description="判断素数的参数")):
    print(num_list)
    num_list = [int(x) for x in num_list.split(",")]
    result = pool.map(is_prime,num_list)
    return dict(zip(num_list,result))



if __name__ == "__main__":
    import uvicorn
    pool = ProcessPoolExecutor()
    uvicorn.run(app=app)


